package com.wudi.estate.controller.tolladministration;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.ownermanagement.Pet;
import com.wudi.estate.bean.tolladministration.Charges;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.tolladministration.ChargesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.*;

@Controller
@RequestMapping("/charges")
@SessionAttributes(value = "communities")
public class ChargesController  {
    @Autowired
    private ChargesService chargesService;
    @Autowired
    private CommunityService communityService;
    @RequestMapping("/listUi")
    public String charges_listUi(String id,ModelMap map){
        List<Community> communities = null;
        try {
            communities = communityService.select();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if(id==null){
            try {
                List<Charges> charges=chargesService.selectAll();
                List<Map<String, Object>> chargesAndComm = insertList(charges);
                map.addAttribute("chargesAndComm",chargesAndComm);
                map.addAttribute("communities",communities);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            try {
                List<Charges> charges=chargesService.selectbyName(id);
                List<Map<String, Object>> chargesAndComm = insertList(charges);
                map.addAttribute("chargesAndComm",chargesAndComm);
                map.addAttribute("communities",communities);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return "charges_list";
    }
    public List<Map<String,Object>> insertList(List<Charges> charges) throws Exception {
        List<Map<String,Object>> chargesAndComm = new ArrayList<>();
        for (Charges c:charges) {
            Map<String,Object> m = new HashMap<>();
            Community community = communityService.selectById(c.getCid());
            m.put("charge",c);
            m.put("community",community);
            chargesAndComm.add(m);
        }
        return chargesAndComm;
    }
    @RequestMapping("/addUi")
    public String charges_addUi(){
        return "charges_add";
    }
    @RequestMapping("/add")
    public String charges_add(Charges charges){
        try {
            charges.setCreateTime(new Date());
            chargesService.insert(charges);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/charges/listUi";
    }
    @RequestMapping("/editUi")
    public String charges_editUi(String id,ModelMap map){
        Charges charges = new Charges();
        charges.setId(id);
        Charges charge1 = new Charges();
        try {
            charge1 = chargesService.selectById(charges);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("charge",charge1);
        return "charges_edit";
    }
    @RequestMapping("/edit")
    public String charges_edit(Charges charges){
        System.out.println(charges);
        try {
            charges.setCreateTime(new Date());
            chargesService.updateById(charges);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/charges/listUi";
    }
    @RequestMapping("/update")
    public String pet_update_state(String id){
        Charges charges = new Charges();
        charges.setId(id);
        try {
            chargesService.updateStateById(charges);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/charges/listUi";
    }
}
